//
//  RadioTableViewCell.m
//  item-NetEaseNews-tyw
//
//  Created by tyw on 15/10/5.
//  Copyright (c) 2015年 tyw. All rights reserved.
//

#import "RadioTableViewCell.h"
#import "RadioModel.h"
#import "UIImageView+WebCache.h"

@implementation RadioTableViewCell {
    UIImageView* iconImgView;       //左上角的小图标
    UILabel* radioTypeLabel;        //电台类型，原创、资讯···
    
//    UIView* view1;          //第一个模块
//    UIView* view2;          //第二个模块
//    UIView* view3;          //第三个模块
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.userInteractionEnabled = YES;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self _createSubViews];
    }
    
    return self;
}

- (void)_createSubViews {
    //左上角小图标
    iconImgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 5, 20, 20)];
    iconImgView.image = [UIImage imageNamed:@"achi_avatar"];
    [self.contentView addSubview:iconImgView];
    
    //电台类型
    radioTypeLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 5, 40, 20)];
    radioTypeLabel.textColor = [UIColor blackColor];
    radioTypeLabel.textAlignment = NSTextAlignmentCenter;
    radioTypeLabel.font = [UIFont systemFontOfSize:12];
    [self.contentView addSubview:radioTypeLabel];
    
    //右侧的  进入 字样
    UILabel* intoLabel = [[UILabel alloc] initWithFrame:CGRectMake(kWidth-60, 5, 30, 20)];
    intoLabel.text = @"进入";
    intoLabel.textColor = [UIColor grayColor];
    intoLabel.textAlignment = NSTextAlignmentCenter;
    intoLabel.font = [UIFont systemFontOfSize:12];
    [self.contentView addSubview:intoLabel];
    //箭头
    UIImageView* intoImgView = [[UIImageView alloc] initWithFrame:CGRectMake(kWidth-20, 10, 6, 10)];
    intoImgView.image = [UIImage imageNamed:@"lm_cell_detail_indicator"];
    [self.contentView addSubview:intoImgView];
    
    //分割线
    UIImageView* lineImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 29.5, kWidth, 0.5)];
    UIImage* line = [UIImage imageNamed:@"home_channel_bar_line"];
    [line stretchableImageWithLeftCapWidth:1 topCapHeight:1];
    lineImgView.image = line;
    [self.contentView addSubview:lineImgView];
    
    //三个模块
    for (int i = 0; i < 3; i++) {
        UIView* view = [[UIView alloc] initWithFrame:CGRectMake(i*kWidth/3, 30, kWidth/3, 220)];
        view.tag = i+100;
        view.backgroundColor = [UIColor clearColor];
        view.userInteractionEnabled = YES;
        [self.contentView addSubview:view];
        
        //图标
        UIImageView* imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, kWidth/3-20, kWidth/3-20)];
        imgView.tag = 11111;
        imgView.layer.cornerRadius = kWidth/6-10;
        imgView.layer.masksToBounds = YES;
        [view addSubview:imgView];
        //播放按钮
        UIButton* playBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
        [playBtn setImage:[UIImage imageNamed:@"video_list_cell_big_icon"] forState:UIControlStateNormal];
        [playBtn addTarget:self action:@selector(playBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        playBtn.alpha = 0.5;
        playBtn.center = imgView.center;
        playBtn.tag = i+10000;
        [view addSubview:playBtn];
        
        //tName
        UILabel* tNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, imgView.bottom+5, imgView.bounds.size.width, 20)];
        tNameLabel.textColor = [UIColor blackColor];
        tNameLabel.textAlignment = NSTextAlignmentCenter;
        tNameLabel.font = [UIFont systemFontOfSize:12];
        tNameLabel.tag = 22222;
        [view addSubview:tNameLabel];
        
        //title
        UILabel* titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, tNameLabel.bottom, imgView.bounds.size.width, 40)];
        titleLabel.textColor = [UIColor grayColor];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.font = [UIFont systemFontOfSize:10];
        titleLabel.numberOfLines = 2;
        titleLabel.tag = 33333;
        [view addSubview:titleLabel];
        
        //播放次数
        UIImageView* playCountImgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, titleLabel.bottom, 15, 13)];
        playCountImgView.image = [UIImage imageNamed:@"audionews_index_tag"];
        [view addSubview:playCountImgView];
        //
        UILabel* playCountLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, titleLabel.bottom, kWidth/3-60, 15)];
        playCountLabel.textColor = [UIColor grayColor];
        playCountLabel.textAlignment = NSTextAlignmentLeft;
        playCountLabel.font = [UIFont systemFontOfSize:10];
        playCountLabel.tag = 44444;
        [view addSubview:playCountLabel];
    }
}

- (void)playBtnAction:(UIButton*)button {
    NSLog(@"%li", button.tag-10000);
}

- (void)setRadioDataDic:(NSDictionary *)radioDataDic {
    if (_radioDataDic != radioDataDic) {
        _radioDataDic = [radioDataDic copy];
        
        [self setNeedsLayout];
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    //电台类型
    NSString* radioType = _radioDataDic[@"cname"];
    radioTypeLabel.text = radioType;
    
    
    NSArray* array = _radioDataDic[@"tList"];
    if (array.count == 3) {
        //三个模块
        for (int i = 0; i < 3; i++) {
            //01 获得数据
            NSDictionary* dataDic = array[i];
            NSDictionary* modelDic = dataDic[@"radio"];
            
            //02 取得view
            UIView* view = [self.contentView viewWithTag:i+100];
            
            //03 修改UI
            //播放次数
            UILabel* playCountLabel = (UILabel*)[view viewWithTag:44444];
            NSNumber* playCountNum = dataDic[@"playCount"];
            if (playCountNum) {
                CGFloat playCount = [playCountNum floatValue];
                if (playCount > 9999) {
                    playCountLabel.text = [NSString stringWithFormat:@"%.1f万", playCount/10000];
                } else {
                    playCountLabel.text = [NSString stringWithFormat:@"%.0f", playCount];
                }
            }
            
            
            if (modelDic) {
                //model千呼万唤始出来
                RadioModel* model = [[RadioModel alloc] initWithDataDic:modelDic];
                
                //图标
                UIImageView* imgView = (UIImageView*)[view viewWithTag:11111];
                NSString* imgUrl = model.imgUrl;
                if (imgUrl.length) {
                    [imgView sd_setImageWithURL:[NSURL URLWithString:imgUrl]];
                } else imgView.image = [UIImage imageNamed:@"audio_block_bg"];
                
                //tName
                UILabel* tNameLabel = (UILabel*)[view viewWithTag:22222];
                tNameLabel.text = model.tName;
                
                //title
                UILabel* titleLabel = (UILabel*)[view viewWithTag:33333];
                NSString* content = model.title;
                NSAttributedString* attrStr = [Tools attributedStrFromStr:content lineSpace:2];
                titleLabel.attributedText = attrStr;
            }
        }
    }
}

@end
